/*
 * KP ebdesk
 */

package com.ebdesk.controller.masterdata;

import com.ebdesk.model.masterdata.Printer;
import com.ebdesk.model.masterdata.TablePrinterModel;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Zulfikar <zulfikarfull@gmail.com>
 */
public class PrinterController {
    private PrinterManagement printerManagement;
    private String[] columnNames = new String[]{"ID", "IP Address", "Name Printing"};
    private int totalRow;
    
    public PrinterController() {
        try {
            printerManagement = new PrinterManagement();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public TablePrinterModel getTableModelAllPrinter(int pageNumber, int rowsPerPage) throws SQLException {
        return getTableModel(getPrinterManagement().getAllPrinter(pageNumber, rowsPerPage));
    }
    
    public TablePrinterModel getTableModelSearchingPrinterByIP(String ipAddressPrinter, int pageNumber, int rowsPerPage) throws SQLException {
        return getTableModel(getPrinterManagement().SearchingPrinterByIp(ipAddressPrinter, pageNumber, rowsPerPage)); 
    }
    
    public TablePrinterModel getTableModelSearchingPrinterByName(String namaPrinter, int pageNumber, int rowsPerPage) throws SQLException {
        return getTableModel(getPrinterManagement().SearchingPrinterByNama(namaPrinter, pageNumber, rowsPerPage));
    }
    
    
    private TablePrinterModel getTableModel(List<Printer> dataPrinter) {
        Object[][] rowData = new Object[dataPrinter.size()][3];
        
        for (int i = 0; i < dataPrinter.size(); i++) {
            rowData[i][0] = dataPrinter.get(i).getId();
            rowData[i][1] = dataPrinter.get(i).getIp_address_printer();
            rowData[i][2] = dataPrinter.get(i).getNama_printer();
        }
        
        
        return new TablePrinterModel(rowData, columnNames);
    }

    /**
     * @return the printerManagement
     */
    public PrinterManagement getPrinterManagement() {
        return printerManagement;
    }

    /**
     * @return the totalRow
     */
    public int getTotalRow() {
        return this.getPrinterManagement().getTotalRow();
    }
    
}
